/**
 * 
 */
package com.xbang.attendance.web.action.warehouse.exchange;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.stock.Inventory;
import com.xbang.attendance.entity.stock.InventoryExchange;
import com.xbang.attendance.service.InventoryService;
import com.xbang.attendance.service.ProductService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.BaseActionSupport;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class Search extends SearchActionSupport {

	private static final long serialVersionUID = 8792923545485296635L;

	private static final Log logger = LogFactory.getLog(Search.class);

	@Autowired
	private InventoryService inventoryService;

	private Integer id;

	private List<InventoryExchange> inventoryExchanges = new ArrayList<InventoryExchange>();

	private InventoryExchange inventoryExchange;

	public String execute() throws Exception {
		return "search";
	}

	@Action("/warehouse/exchange/${id}/inventory")
	public String viewExchange() throws Exception {

		if (id != null) {
			Inventory inventory = (Inventory) inventoryService.getObject(Inventory.class, id);
			if (inventory != null) {
				String hqlSentence = "FROM InventoryExchange inventoryExchange WHERE 1 = 1 ";
				searchComponent.resetWhereSentence();
				searchComponent.addSearchElement("eq.inventoryExchange.sourceWarehouse.id", inventory.getWarehouse().getId());
				searchComponent.addSearchElement("eq.inventoryExchange.product.id", inventory.getProduct().getId());
				Result result = inventoryService.searchByPage(hqlSentence, searchComponent, page);
				inventoryExchanges = result.getItems();

				if (inventoryExchanges != null && !inventoryExchanges.isEmpty()) {
					inventoryExchange = inventoryExchanges.get(0);
				}

				page = result.getPage();
			}
		}

		return "index";
	}

	/**
	 * @return
	 * @throws Exception
	 */
	@Action("/warehouse/exchange/search/do")
	public String doSearch() throws Exception {
		String hqlSentence = "FROM InventoryExchange inventoryExchange WHERE 1 = 1 ";

		Result result = inventoryService.searchByPage(hqlSentence, searchComponent, page);
		inventoryExchanges = result.getItems();
		page = result.getPage();

		return "index";
	}

	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}

	public InventoryService getInventoryService() {
		return inventoryService;
	}

	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	public List<InventoryExchange> getInventoryExchanges() {
		return inventoryExchanges;
	}

	public void setInventoryExchanges(List<InventoryExchange> inventoryExchanges) {
		this.inventoryExchanges = inventoryExchanges;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the inventoryExchange
	 */
	public InventoryExchange getInventoryExchange() {
		return inventoryExchange;
	}

	/**
	 * @param inventoryExchange
	 *            the inventoryExchange to set
	 */
	public void setInventoryExchange(InventoryExchange inventoryExchange) {
		this.inventoryExchange = inventoryExchange;
	}

}
